/**
 * Created by Administrator on 2017/7/4.
 */

let variable = require('../data/variable.js');
let msg = require('../util/msg.js');
let log = require('../util/log.js');
let call = require('./call.js');
let pay = require('./pay.js');

module.exports = action;

function action(obj, cid) {
    //log.p('action error obj', obj);
    if (!obj.userId) {
        msg.send(cid, obj.uuid, 'error', {'api': obj.action, 'code': 10018});
        return false;
    }
    // console.log(obj);
    let data = obj.data;
    switch (obj.action) {
        case 'gateAuth':
            call.auth.bindGate(cid, obj.data.serverId);
            break;
        //case 'gameAuth':
        //    call.auth.bindGame(cid, obj.data.serverId);
        //    console.log(cid, obj.uuid);
        //    msg.send(cid, obj.uuid, 'gameAuth');
        //    break;
        case 'dcLink':
            call.socket.setUuid(obj.userId, obj.uuid);
            call.socket.setCid(obj.userId, cid); 
            call.socket.setWeixinId(obj.userId, data.weixinId);  
            //call.weixin.putWeixinStatus(obj.userId, variable.blued.statusCreating);
            // call.socket.setBluedId(obj.userId, data.bluedId);
            // call.blued.putBluedStatus(obj.userId, variable.blued.statusCreating);
            call.task.init(obj.userId);
            break;
        case 'dcUnLink':
            //call.weixin.putWeixinStatus(obj.userId, variable.blued.statusExit);
            // call.blued.putBluedStatus(obj.userId, variable.blued.statusExit);
            call.socket.clear(obj.userId);
            call.task.clear(obj.userId);
            call.online.logout(obj.userId);
            break;
        case 'addGame':
            //log.p('addGame:', data);
            call.gameLists.addGame(data);
            break;
        case 'delGame':
            //log.p('delGame', data.gameId);
            call.gameLists.delGame(data.gameId);
            break;
        case 'updateGame':
            //log.p('updateGame', data);
            call.gameLists.updateGame(data);
            break;
        case 'updateGamePraise':
            call.gameLists.updateGamePraise(data);
            break;
        case 'setGamePraise':
            call.gameLists.setGamePraise(data);
            break;
        case 'addRecruit':
            //log.p('addRecruit', data);
            call.gameLists.addRecruit(data);
            break;
        case 'delRecruit':
            //log.p('delRecruit', data.gameId);
            call.gameLists.delRecruit(data.gameId);
            break;
        case 'getGameAmount':
            let amount = call.gameLists.amount();
            msg.send(cid, obj.uuid, 'getGameAmount', amount);
            break;
        case 'getRecruit':
            call.gameLists.getRecruit(data.bluedId, function (game) {
                msg.send(cid, obj.uuid, 'getRecruit', game);
            });
            break;
        case 'syncGame':
            call.gameLists.syncGame(data);
            break;
        case 'getGames':
            let getGames = call.gameLists.getGames();
            msg.send(cid, obj.uuid, 'getGames', getGames);
            break;
        case 'killGame':
            call.gameLists.killGame(data);
            msg.send(cid, obj.uuid, 'killGame', {'kill': true});
            break;
        case 'getRobot':
            if (!call.robot.check(data.lv)) {
                msg.send(cid, obj.uuid, 'getRobot', null);
                return false;
            }
            call.robot.apply(data.lv, function (robotId) {
                if (robotId.length < 2) {
                    msg.send(cid, obj.uuid, 'getRobot', null);
                    return false;
                }
                msg.send(cid, obj.uuid, 'getRobot', robotId);
            });
            break;
        case 'setWeixin':
            if (!data.uid) {
                return false;
            }   
            if (data.accessToken && data.expire && data.refreshToken) {
                call.weixin.setAccessToken(data.uid, data.accessToken, data.expire, data.refreshToken);
            }   
            if (data.platform) {
                call.weixin.setPlatform(data.uid, data.platform);
            }   
            break;
        case 'getWeixin':
            let result2 = {};
            if (data.uid) {
                result2.platform = call.weixin.getPlatform(data.uid);
                result2.accessToken = call.weixin.getAccessToken(data.uid); 
                result2.refreshToken = call.weixin.getRefreshToken(data.uid);  
            }   
            msg.send(cid, obj.uuid, 'getWeixin', result2);
            break;
        case 'setTicket':
            if (!data.uid) {
                return false;
            }   
            if (data.ticket && data.expire) {
                call.weixin.setTicket(data.uid, data.ticket, data.expire);
            }   
            break;
        case 'getTicket':
            let result3 = {};
            if (data.uid) {
                result3.ticket = call.weixin.getTicket(data.uid);  
            }   
            msg.send(cid, obj.uuid, 'getWeixin', result3);
            break;
        case 'setBlued':
            if (!data.uid) {
                return false;
            }
            if (data.accessToken && data.expire) {
                call.blued.setAccessToken(data.uid, data.accessToken, data.expire);
            }
            if (data.platform) {
                call.blued.setPlatform(data.uid, data.platform);
            }
            break;
        case 'getBlued':
            let result = {};
            if (data.uid) {
                result.platform = call.blued.getPlatform(data.uid);
                result.accessToken = call.blued.getAccessToken(data.uid);
            }
            msg.send(cid, obj.uuid, 'getBlued', result);
            break;
        case 'putBluedStatus':
            let userId = null;
            if (obj.userId == -1 && data.userId) {
                userId = data.userId;
            } else if (obj.userId > 0) {
                userId = obj.userId;
            }
            if (!userId) {
                return false;
            }
            call.blued.putBluedStatus(userId, data.status);
            break;
        case 'payCallback':
            pay.callback(data);
            break;
        case 'closeOrder':
            pay.closeOrder(data);
            break;
        case 'addItem':
            call.package.addItem(data.userId, data.itemAddId, function (result) {
                msg.send(cid, obj.uuid, 'addItem', result);
            });
            break;
        case 'taskList':
            call.task.list(data.userId, function (result) {
                msg.send(cid, obj.uuid, 'taskList', result);
            });
            break;
        case 'taskUpdate':
            call.task.update(data.userId, data.mode, data.add);
            //call.task.update(data.userId, data.taskId, data.num);
            break;
        case 'taskFinish':
            call.task.finish(data.userId, data.taskId);
            //call.task.finish(data.userId, data.taskId, function (result) {
            //    msg.send(cid, obj.uuid, 'taskFinish', result);
            //});
            break;
        case 'alertMsg':
            call.auth.alertMsg(data.text);
            msg.send(cid, obj.uuid, 'alertMsg', {'send': true});
            break;
        case 'scoreList':
            call.user.list(data.userId, function (result) {
                msg.send(cid, obj.uuid, 'taskList', result);
            });
            break;
    }
}